<!DOCTYPE html>
<html>
	<head>
		<title>Image Recovery Script</title>
		<meta http-equiv="content-type" content="text/html;charset=utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<!-- <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">-->
		<link rel="stylesheet" href="/css/bootstrap.min.css">
		<script src="/js/jquery.min.js"></script>
		<script src="/js/bootstrap.min.js"></script>
		</head>
	<body>
<?php
	extract($_POST);
	extract($_GET);
	unset($_POST,$_GET);

	$onlySSD=0;

	if(isset($ssdsize)) $ssdsize=trim($ssdsize);
	if(isset($hddsize)) $hddsize=trim($hddsize);
	if(isset($osname)) $osname=trim($osname);
	if(isset($rename)) $rename=trim($rename);
	if(isset($dataname)) $dataname=trim($dataname);

	if(isset($hddsize) && strlen($hddsize)>2) {
		$onlySSD=0;
	}else{
		$onlySSD=1;
	}

	if(isset($ishaier)){
		$efi_size=260;
		$recovery_size=1024;
		$MsgHaier=" this script is For haier";
	}else{
		$efi_size=100;
		$recovery_size=900;
	}

	echo "	<form action='Image_script.php?act=q' method='POST' role='form' class='form-inline'>";
	if(!isset($ssdsize)){
		echo "		<div class='form-group'>";
		echo "			<label for='STOR' class='col-sm-4 form-control'>磁盘大小</label>";
		echo "			<input type='text' class='form-control' name='ssdsize' id='STOR' placeholder='请输入SSD实际大小'>";
		echo "			<input type='text' class='form-control' name='hddsize' id='STOR' placeholder='请输入HDD实际大小'>";
		echo "</div>";
		echo "		<div class='form-group'>";
		echo "			<label for='IMGNAME' class='col-sm-4 form-control'>镜像文件</label>";
		echo "			<input type='text' class='form-control' name='osname' id='IMGNAME' placeholder='请输入OS文件名'>";
		echo "			<input type='text' class='form-control' name='rename' id='IMGNAME' placeholder='请输入WINRE文件名'>";
		echo "			<input type='text' class='form-control' name='dataname' id='IMGNAME' placeholder='请输入数据分区文件名'>";
		//echo "			海尔: <input type='checkbox' name='ishaier' value='1' />\n";
		echo "			<button type='submit' class='btn btn-info form-control'>生成脚本</button>";
		echo "		</div>";
	}else{
		echo "		<div class='form-group'>";
		echo "			<label for='STOR' class='col-sm-4 form-control'>磁盘大小</label>";
		echo "			<input type='text' class='form-control' name='ssdsize' id='STOR' placeholder='请输入SSD实际大小' value='${ssdsize}'>";
		echo "			<input type='text' class='form-control' name='hddsize' id='STOR' placeholder='请输入HDD实际大小' value='${hddsize}'>";
		echo "</div>";
		echo "		<div class='form-group'>";
		echo "			<label for='IMGNAME' class='col-sm-4 form-control'>镜像文件</label>";
		echo "			<input type='text' class='form-control' name='osname' id='IMGNAME' placeholder='请输入OS文件名' value='${osname}'>";
		echo "			<input type='text' class='form-control' name='rename' id='IMGNAME' placeholder='请输入WINRE文件名' value='${rename}'>";
		echo "			<input type='text' class='form-control' name='dataname' id='IMGNAME' placeholder='请输入数据分区文件名' value='${dataname}'>";
		//if(isset($ishaier))
		//	echo "			海尔: <input type='checkbox' name='ishaier' value='1' checked='checked' />\n";
		//else
		//	echo "			海尔: <input type='checkbox' name='ishaier' value='1' />\n";
		echo "			<button type='submit' class='btn btn-info form-control'>生成脚本</button>";
		echo "		</div>";
	}
	echo "	</form>";

$Msg=date("Y-m-d H:i:s").$MsgHaier;

if(isset($act)){

echo "<textarea rows='50' cols='116' readonly='readonly'>
@echo off
::+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
::Config Area   -- Create on ${Msg}
::+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
::  Define the image and data to recovery
	SET SOURCEImage=${osname}
	SET REIMAGE=${rename}
	SET HDDATA=${dataname}

::: DISK Size
	SET SSD_SIZE=${ssdsize}
	SET HDD_SIZE=${hddsize}

::: Only for apply test image
	SET TEST_IMAGE=
::+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

:CheckDiskAndSelect
	echo list disk > list.txt
	echo exit >> list.txt
	diskpart /s list.txt >diskinfo.txt

	set disk=
	set DDisk=
	set GDisk=

	set ErrMsg=Cannot find matched Disk

:disk0
	find \"Disk 0    Online\" diskinfo.txt | find /i \"%SSD_SIZE%\"
	if %errorlevel% == 0 set disk=0&goto CheckHDDDisk

:disk1
	find \"Disk 1    Online\" diskinfo.txt  | find /i \"%SSD_SIZE%\"
	if %errorlevel% == 0 set disk=1&goto CheckHDDDisk

	if not defined disk goto Fail

:CheckHDDDisk

";

if(!$onlySSD){
echo "
	find \"Disk 0    Online\" diskinfo.txt | find /i \"%HDD_SIZE%\"
	if %errorlevel% == 0 set DDisk=0&set GDisk=2

	find \"Disk 1    Online\" diskinfo.txt | find /i \"%HDD_SIZE%\"
	if %errorlevel% == 0 set DDisk=1&set GDisk=1

	if not defined DDisk goto Fail
";
}
///////////////////////SSD ONLY Area //////////////////////////////////////
if($onlySSD){
echo "
:SSDOnly";

if($ssdsize > 0 && $ssdsize <= 512)
echo "
	set OS_PART_SIZE=102407
";

if($ssdsize > 512 && $ssdsize <= 2048)
echo "
	set OS_PART_SIZE=204806
";

echo "
	set ErrMsg=Can't create SSD partitions
	color 6f
	::select disk
	echo OS DISK ID is %DISK%
	rem convert distination partition to GPT mode
	echo select disk \"%DISK%\" > script.txt
	echo clean >> script.txt
	echo convert gpt >> script.txt

	rem create ESP partition
	echo create partition efi size=${efi_size} >> script.txt
	echo format fs=fat32 quick override >> script.txt
	echo assign letter=\"S\" >> script.txt

	rem create MSR partition
	echo create partition msr size=16 >> script.txt
	echo create partition primary size=%OS_PART_SIZE% >> script.txt
	echo format fs=ntfs quick override >> script.txt
	echo assign letter=\"W\" >> script.txt

	echo create partition primary >> script.txt
	echo shrink minimum=${recovery_size} >> script.txt
	echo format fs=ntfs quick override >> script.txt
	echo assign letter=\"T\" >> script.txt

	echo create partition primary >> script.txt
	echo format fs=ntfs quick override  >> script.txt
	echo assign letter=\"R\" >> script.txt
	echo set id=\"de94bba4-06d1-4d40-a16a-bfd50179d6ac\" >> script.txt
	echo gpt attributes=0x8000000000000001 >> script.txt

	rem script finish
	echo exit >> script.txt
	type script.txt
	rem run above script with diskpart
	diskpart /s script.txt
	if not %errorlevel%==0 goto Fail
";
}else{
///////////////////////SSD+HDD ONLY Area //////////////////////////////////////
echo "
:SSD_HDD
	set ErrMsg=Can't create SSD or HDD partitions
	color 6f
	::select disk
	echo OS DISK ID is %DISK%
	rem convert distination partition to GPT mode
	echo select disk %DISK% > script.txt
	echo clean >> script.txt
	echo convert gpt noerr >> script.txt

	rem create ESP partition
	echo create partition efi size=${efi_size} >> script.txt
	echo format fs=fat32 quick override >> script.txt
	echo assign letter=\"S\" >> script.txt

	rem create MSR partition
	echo create partition msr size=16 >> script.txt

	echo create partition primary >> script.txt
	echo shrink minimum=${recovery_size} >> script.txt
	echo format fs=ntfs quick override >> script.txt
	echo assign letter=\"W\" >> script.txt

	echo create partition primary >> script.txt
	echo format fs=ntfs quick override  >> script.txt
	echo assign letter=\"R\" >> script.txt
	echo set id=\"de94bba4-06d1-4d40-a16a-bfd50179d6ac\" >> script.txt
	echo gpt attributes=0x8000000000000001 >> script.txt

	rem script finish
	echo exit >> script.txt
	type script.txt
	rem run above script with diskpart
	diskpart /s script.txt

	rem HDD partition
	echo DATA DISK ID is %DDISK%
	echo select disk %DDISK% > script.txt
	echo clean >> script.txt
	echo convert mbr noerr >> script.txt
";

if($hddsize>0 && $hddsize <= 1024)
echo "
	echo create partition primary size=204806>> script.txt
	echo format fs=ntfs quick override >> script.txt
	echo assign letter=\"Y\" >> script.txt

	echo create partition primary size=307204>> script.txt
	echo format fs=ntfs quick override >> script.txt
	echo assign letter=\"T\" >> script.txt
";

if($hddsize>1024 && $hddsize <= 2048)
echo "
	echo create partition primary size=512002>> script.txt
	echo format fs=ntfs quick override >> script.txt
	echo assign letter=\"Y\" >> script.txt

	echo create partition primary size=512002>> script.txt
	echo format fs=ntfs quick override >> script.txt
	echo assign letter=\"T\" >> script.txt
";

echo "
	echo create partition primary>> script.txt
	echo format fs=ntfs quick override >> script.txt
	echo assign letter=\"Z\" >> script.txt
	type script.txt
	diskpart /s script.txt
	if not %errorlevel%==0 goto Fail
";
}

echo "
:FINDSOURCE
	color
	rem find source file in root directory
	set ErrMsg=Can't find source image file
	if not exist \"%Drv%\%SOURCEImage%\" goto Fail
	set source=%Drv%

:MAKEDIR
	rem prepare directory for recovery funtion
	:md W:\Recycler\Scratch

:APPLYOSIMAGE
	color 1f
	rem start to apply image
	set ErrMsg=Apply image file fail
	if exist script.txt del script.txt
	cls
	echo =============== Applying Image, please wait ===================
	echo.
	echo oooooo   oooooo    oooo       .o.       ooooo ooooooooooooo
	echo `888.    `888.     .8'       .888.      `888' 8'   888   `8
	echo  `888.   .8888.   .8'       .8\"888.      888       888
	echo   `888  .8'`888. .8'       .8' `888.     888       888
	echo    `888.8'  `888.8'       .88ooo8888.    888       888
	echo     `888'    `888'       .8'     `888.   888       888
	echo      `8'      `8'       o88o     o8888o o888o     o888o
	echo.
	echo ===============================================================
	echo.
	echo Restore OS Image(%SOURCEImage%) to W:\ .........................
	dism /apply-image /imagefile:\"%source%\%SOURCEImage%\" /index:1 /applydir:W:\
	if not %errorlevel%==0 goto Fail

	if defined TEST_IMAGE W:\Windows\System32\bcdboot W:\Windows /s S: /f all&goto MakeBoot

:ApplyData
	echo.
	echo Restore DATA(%HDDATA%) to T:\ .........................
	set ErrMsg=Apply data Partition fail
	dism /apply-image /imagefile:\"%source%\%HDDATA%\" /index:1 /applydir:T:\
	if not %errorlevel%==0 goto Fail

:CopyWinRe
	echo.
	echo Restore WinRE to R:\ .........................
	set ErrMsg=Apply reimage files fail
	dism /apply-image /imagefile:\"%source%\%REIMAGE%\" /index:1 /applydir:R:\
	if not %errorlevel%==0 goto Fail

:MakeBoot
	set ErrMsg=Write system boot files fail
	W:\Windows\System32\bcdboot W:\Windows /s S: /f all
	if not %errorlevel%==0 goto Fail

:SetRe
	set ErrMsg=Set WinRE image file fail
	W:\Windows\System32\Reagentc /setreimage /path R:\Recovery\WindowsRE /target W:\Windows
	if not %errorlevel%==0 goto Fail

:CopyFiles
	echo. =============== WAIT SYSTEM COPY FILES ================
	if EXIST %source%\Scripts\Unattend.xml copy %source%\Scripts\Unattend.xml w:\Windows\Panther /y
	if EXIST %source%\Scripts\*.ppkg copy %source%\Scripts\*.ppkg c:\Recovery\Customizations /y

goto Pass

:Fail
	color cf
	echo #################################################
	echo %ErrMsg%
	echo #################################################
	echo oooooooooooo       .o.       ooooo ooooo        
	echo `888'     `8      .888.      `888' `888'        
	echo  888             .8\"888.      888   888         
	echo  888oooo8       .8' `888.     888   888         
	echo  888    \"      .88ooo8888.    888   888         
	echo  888          .8'     `888.   888   888       o 
	echo o888o        o88o     o8888o o888o o888ooooood8 
	echo.
	pause
goto Fail

:Pass
	COLOR 2F
	echo ooooooooo.         .o.        .oooooo..o  .oooooo..o 
	echo `888   `Y88.      .888.      d8P'    `Y8 d8P'    `Y8 
	echo  888   .d88'     .8\"888.     Y88bo.      Y88bo.      
	echo  888ooo88P'     .8' `888.     `\"Y8888o.   `\"Y8888o.  
	echo  888           .88ooo8888.        `\"Y88b      `\"Y88b 
	echo  888          .8'     `888.  oo     .d8P oo     .d8P 
	echo o888o        o88o     o8888o 8\"\"88888P'  8\"\"88888P'  
	echo.
	echo.
	choice /C YN /M \"Wanna reboot?\"
	if %errorlevel% == 1 goto Reboot
	if %errorlevel% == 2 goto PowerOff

:PowerOff
	wpeutil shutdown
	Pause
goto PowerOff

:Reboot
	wpeutil reboot
	Pause
goto Reboot</textarea>
";
}
?>
	</body>
</html>